#' ---
#' title: "Additional Sources"
#' author: "Lukas F. Stoetzer"
#' date: "November 2020"
#' ---



# COVID Cases =============

  # Load Data  
  dat_cases <- read_csv("dat_owid-covid-data.csv") %>%
                filter(location %in% c("United States","Germany","Poland","Italy","Brazil")) %>%
                rename(cntry = "location")

  # Load Responses
  dat_surv <- readRDS("dat_wide.RDS")
  
  field_period <- dat_surv %>% 
    rename(cntry = "cntry_resp") %>%
    mutate(cntry = case_when(cntry == "pol" ~ "Poland",
                             cntry == "bra" ~ "Brazil",
                             cntry == "ger" ~ "Germany",
                             cntry == "usa" ~ "United States",
                             cntry == "ita" ~ "Italy")) %>%
    group_by(cntry) %>%
    summarise(start_field = min(RecordedDate),
              end_field = max(RecordedDate))
  
    
  # Plot
  ggplot() +
    geom_line(data = dat_cases,
              aes(x=date,y=(new_cases_smoothed_per_million), 
                  group=cntry),size=0.8) +
    facet_wrap(~ cntry) +
    geom_rect(data = field_period,
              aes(xmin = as.Date(start_field),
                  xmax = as.Date(end_field),
                  ymin = -Inf, ymax = Inf), alpha = .2)+
    ylab("New Cases Per Million") +
    theme_bw()  
    

  # Save Figure
  ggsave("appendix_figure3.pdf",width=12,height=9)

# Intensive Care Units =============
  
  # https://www.oecd.org/coronavirus/en/data-insights/intensive-care-beds-capacity
  df_IUC <- read_csv("dat_IUCBeds_OECD22.csv") %>%
    mutate(sample = case_when(
      Country %in% c("Germany","Poland","Italy","United States") ~ "Cases in Sample",
      TRUE ~ "Other"
    ),
    CntryYear = paste(Country, " (", Year,")", sep="")) 
    

  # Plot
  ggplot(df_IUC) +
    geom_point(aes(y=reorder(CntryYear,IUC),x=IUC, col = sample), size=2) +
    xlab("Intensive care beds per 100 000 population") + ylab("") +
    theme_bw() + 
    scale_color_grey() +
    theme(text = element_text(size=12),
          legend.position = "none")
  
  # Save Figure
  ggsave("appendix_figure4.pdf",height=9)


    
# Party Positions =============

  # Set API Key  
  mpds <- read_csv("dat_MPDataset_MPDS2020a.csv")
  mpdssa <- read_csv("dat_MPDataset_MPDSSA2019b.csv")
  
  # Select
  mpds_sel <- bind_rows(mpds,mpdssa) %>% 
    filter(countryname %in% c("United States","Germany","Brazil","Italy","Poland"),
           date > 201500) %>%
    mutate(logrile = mp_scale(., scalingfun = logit_rile),
           CountryYear = paste(countryname, " (",substring(date,0,4),")",sep=""),
           party_sel = case_when(
           party %in% c("32720","41953","92436","61620","180230") ~ "Conjoint Party",
           party %in% c("41113","92435","61320","180620","32440") ~ "Conjoint Party",
           TRUE ~ "Other Parties"))  %>%
    # Create Identity Dimension
    mutate(
      cul_pos = per601 + per603 + per607, #  national way of life, traditional morality, and multiculturalism
      cul_neg = per602 + per604 + per608,
      cul_rile =  (cul_pos - cul_neg)/(cul_pos + cul_neg),
      pervote = case_when(countryname == "Brazil" ~ presvote,
                          TRUE ~ pervote)
    )
  

  ggplot(filter(mpds_sel, pervote >= 5)) +
   geom_hline(yintercept = 0, col="grey") +
    geom_point(aes(y=0,x=rile, size=pervote,col=party_sel)) +
    geom_text_repel(aes(y=0,x= rile, label = partyname,col=party_sel),
              nudge_y = -1,angle = 0, hjust = 0.4) +
    facet_grid(countryname~.)    +
    ylim(-2,1) + xlab("Position on RILE Index") +
    scale_color_grey() +
    theme(
      panel.background = element_rect(fill = "transparent",colour = NA),
      plot.title = element_text(size = 16, hjust = 0.5, vjust = 1),
      plot.background = element_rect(fill = "transparent"),
      axis.title.y=element_blank(),
      axis.text.y = element_blank(),
      axis.ticks.y = element_blank(),
      legend.position = 'bottom',
      legend.title=element_blank(),
      legend.text=element_text(size=15),
      legend.background = element_rect(fill = "transparent")
    )
  
  # Save Figure
  ggsave("appendix_figure5.pdf",height=8, width=9)
  
  